<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');


/**
 * Image cache and resize helper
 * 
 * @author Ali OYGUR <alioygur@gmail.com>
 * @date   2012-02-14
 *
 * @param string the image file path
 * @param int image with
 * @param int image height
 * @return string the cached image file path
 */
function resize($file, $w = 80, $h = 80)
{
	// cache file dir
	$cache_path = 'upload/cache/';
	
	// if file exists
	if(!file_exists($file))
		exit('file not found!');

	// generate cached file name
	
	$finfo = pathinfo($file);
	
	$cached_file = $cache_path.md5($file.$w.$h).'.'.$finfo['extension'];

	// if cache exists return
	if(file_exists($cached_file))
		return $cached_file;
	
	// else create cache file
	
	touch($cached_file);

	try{
		$img = new Imagick($file);
		$img->cropThumbnailImage($w, $h);
		$img->writeImage($cached_file);
		$img->clear();
	} catch(ImagickException $e){
		return $e->getMessage();
	}

	return $cached_file;
}

/* End of file image_helper.php */
